package abq2.jic.jic.plugin.operate.teacherEvaluate;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Date;
import java.util.EventObject;
import java.util.List;

public class EvaluateConfirm extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        Object primaryKey = this.getView().getFormShowParameter().getCustomParam("teacherId");
        if (primaryKey != null) {
            this.getModel().setValue("abq2_teacher_info",primaryKey);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        if (source.getKey().equals("btnok")){
            Object primaryKey = this.getView().getFormShowParameter().getCustomParam("teacherId");
            QFilter idFilter = new QFilter("id", QCP.equals,primaryKey);


            DynamicObject evaluateResult = BusinessDataServiceHelper.newDynamicObject("abq2_evaluate_result");
            StringBuilder sb1 = new StringBuilder(); //生成一个单据编号
            sb1.append("EvaluateResult-");
            for (int i = 1; i <= 10; i++) {
                int ascii = 48 + (int) (Math.random() * 9);
                char c = (char) ascii;
                sb1.append(c);
            }

            DynamicObject teacherInfo = BusinessDataServiceHelper.loadSingle("abq2_teacher_info",
                    "id,name,abq2_teacherid"
                    , new QFilter[]{new QFilter("id", QCP.equals, primaryKey)});

            evaluateResult.set("number",sb1);
            evaluateResult.set("abq2_teacher_name",teacherInfo.getString("name"));
            evaluateResult.set("status","A");
            evaluateResult.set("abq2_evaluate_date",new Date());
            evaluateResult.set("creator",RequestContext.get().getCurrUserId());
            evaluateResult.set("abq2_teacherid",teacherInfo.getString("abq2_teacherid"));
            evaluateResult.set("abq2_evaluate_score",this.getModel().getValue("abq2_evaluate_score"));
            evaluateResult.set("abq2_evaluate_context",this.getModel().getValue("abq2_evaluate_context"));

            OperationResult saveOperate = SaveServiceHelper.saveOperate("abq2_evaluate_result", new DynamicObject[]{evaluateResult}, null);
            List<Object> successPkIds = saveOperate.getSuccessPkIds();

            System.out.println(successPkIds.get(0));
            this.getView().returnDataToParent(true);
            this.getView().close();
        }
    }
}
